import json
import os.path
import openpyxl
from Tools.scripts.make_ctype import values
from openpyxl.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet
from common.doLog import DoLog
dl = DoLog()
myLogger = dl.getLogger(__name__)
my_path=r'E:\work\codings\python\2506\autoOne\datas'
class doFile():
    #txt文件的读
    def txtRead(self):
        fileName=r"E:\work\codings\python\2506\autoOne\wo.text.txt"
        try:
            print('我要开始执行txtRead了')
            with open(fileName,encoding='utf_8') as f:
                print(f, type(f))
                print(f.read())
        except Exception as A:
            print(f"我发生异常了，异常类型是{A}")
        else:
            print('未发生异常！')
        finally:
            print('不知道是否发生异常，运算已结束')
        #txt文件的写，追加
    def txtWrite(self):
        fileName = r"E:\work\codings\python\2506\pybase\codings\wo1.text.txt"
        try:
            print('我要开始执行txtWrite了')
            with open(fileName,mode='a',encoding='utf_8') as f:
                print(f, type(f))
                print(f.write('玛卡巴卡，天线宝宝'))
        except Exception as A:
            print(f"我发生异常了，异常类型是{A}")
        else:
            print('未发生异常！')
        finally:
            print('不知道是否发生异常，运算已结束')
    #json的读
    def jsonRead(self):
        fileName = r"E:\work\codings\python\2506\pybase\codings\text.json"
        try:
            print('我要开始执行jsonRead了')
            myLogger.info('我要开始执行jsonRead了')
            with open(fileName, encoding='utf_8') as f:
                print(f, type(f))
                print(f.read())
        except Exception as A:
            print(f"我发生异常了，异常类型是{A}")
            myLogger.exception(f"我发生异常了，异常类型是{A}")
        else:
            print('未发生异常！')
            myLogger.info('未发生异常！')
        finally:
            print('不知道是否发生异常，运算已结束')
            myLogger.info('不知道是否发生异常，运算已结束')
    # json的写
    def jsonWrite(self):
        fileName = r"E:\work\codings\python\2506\pybase\codings\text.json"
        try:
            print('我要开始执行jsonWrite了')
            myLogger.info('我要开始执行jsonWrite了')
            with open(fileName, mode='a', encoding='utf_8') as f:
                print(f, type(f))
                print(f.write('玛卡巴卡，天线宝宝'))
        except Exception as A:
            print(f"我发生异常了，异常类型是{A}")
            myLogger.exception(f"我发生异常了，异常类型是{A}")
        else:
            print('未发生异常！')
            myLogger.info('未发生异常！')
        finally:
            print('不知道是否发生异常，运算已结束!')
            myLogger.info('不知道是否发生异常，运算已结束!')

    def myReadpng(self):
        fileName=r'E:\work\codings\python\2506\pybase\codings\tuPian.png'
        with open(fileName, mode='r+b') as f:
            print(f, type(f))
            readPng=f.read()
            return readPng
    def myWritepng(self,fname,strs):
        fileName=os.path.join(my_path,fname)
        with open(fileName, mode='w+b') as f:
            f.write(strs)
    #将字典转成json字符串
    def doDictTOJsonStr(self,d):
        s=json.dumps(d)
        print(s,type(s))
        return s
    #将json字符串转为字典
    def doJsonTODicStr(self,jsonS):
        b=json.loads(jsonS)
        print(b,type(b))
        return b
    #将字典内容保存为json格式文件
    def doDictTOJsonFile(self,d,fname):
        fileName = os.path.join(my_path, fname)
        with open(fileName, mode='a', encoding='utf_8') as f:
            json.dump(d,f)
    #将json格式文件提取出并转成字典
    def doJsonTODictFile(self,fname):
        fileName = os.path.join(my_path, fname)
        with open(fileName, mode='r', encoding='utf_8') as f:
            d=json.load(f)
            print(d,type(d))
            return d
    #处理excel文件，读，写
    def doExcelRead(self,fname):
        fileName = os.path.join(my_path, fname)
        wk:Workbook=openpyxl.load_workbook(fileName)
        st:Worksheet=wk['r']
        print(st,type(st))
        ss=st.iter_rows(values_only=True)
        print(ss)
        print(range(10))
        l=[i for i in ss]
        print(f'{l}')
        return l
    # 处理excel文件部分数据，读
    def doExcelReadDIV(self, fname,stname,minRow=None,maxRow=None,minCol=None,maxCol=None):

        fileName = os.path.join(my_path, fname)
        try:
            print('doExcelReadDIV开始执行了！')
            myLogger.info('doExcelReadDIV开始执行了！')
            wk: Workbook = openpyxl.load_workbook(fileName)
            st: Worksheet = wk[stname]
            print(st, type(st))
            ss = st.iter_rows(min_row=minRow, max_row=maxRow, min_col=minCol, max_col=maxCol, values_only=True)
        except Exception as A:
            print(f"我发生异常了，异常类型是{A}")
            myLogger.exception(f"我发生异常了，异常类型是{A}")
        else:
            print('未发生异常！')
            myLogger.info('未发生异常！')
            l = [i for i in ss]
            print(f'{l}')
            return l
        finally:
            print('运行结束！')
            myLogger.info('运行结束！')
            wk.close()
    #写execl文件
    # excel的写
    def doExcelWrite(self,fname,stname,ss):
        fileName = os.path.join(my_path, fname)#新文件名
        try:
            print("我要开始执行doExcelWrite了")
            myLogger.info("我要开始执行了！")
            wk=openpyxl.Workbook()#创建新文件
            st:Worksheet=wk.create_sheet(stname,1)#创建sheet对象
            st.cell(1,1,ss)#写一个单元格
            wk.save(fileName)#保存
        except Exception as A:
            print(f"我发生异常了，异常类型是{A}")
            myLogger.exception(f"我发生异常了，异常类型是{A}")
        else:
            print('未发生异常！')
            myLogger.info('未发生异常！')
        finally:
            print('不知道是否发生异常，运算已结束')
            myLogger.info('不知道是否发生异常，运算已结束')
            wk.close()
if __name__=='__main__':
    df=doFile()
    # df.txtRead()
    # df.txtWrite()
    # df.jsonRead()
    # df.jsonWrite()
    # df.doExcelWrite('表格.xlsx','mython','玛卡巴卡')
    # df.doExcelRead('运算用例.xlsx')
    # df.doExcelReadDIV('运算用例.xlsx','jia',2,18,2,5)
    # df.doExcelReadDIV('运算用例.xlsx', 'jian', 2, 20, 2, 5)
    df.doExcelReadDIV('运算用例.xlsx', 'cheng', 2, 20, 2, 5)
    # df.doExcelReadDIV('运算用例.xlsx', 'chu', 2, 21, 2, 5)
    # r= {'name': 'zhangsan','age':18}
    # df.doDictTOJsonStr(r)
    # df.doJsonTODicStr(df.doDictTOJsonStr(r))
    # s={'name': 'zhangsan','age':18}
    # df.doDictTOJsonFile(s,'text.json')
    # print(df.myReadpng())
    # s=df.myReadpng()
    # fileName="tupian2.png"
    # df.myWritepng(fileName,s)